package weaver.tools.util;

import java.math.BigDecimal;

/**
 * @author dusongsong
 * @date 2024年04月16日 10:56
 */
public class TransMethodUtil {
    /*
     * clob 字段转成拼接字符串型
     */
    public static String clob2String(String content) {
        content = content.replaceAll("'", "\"\"");
        String contentString = "";
        int clobCount = content.length() / 2000;
        int remain = content.length() % 2000;
        for (int a = 0; a < clobCount; a++) {
            contentString += "to_clob('" + content.substring(a * 2000, (a + 1) * 2000) + "')||";
        }
        if (remain > 0) {
            contentString += "to_clob('" + content.substring(clobCount * 2000, clobCount * 2000 + remain) + "')||";
        }
        if (!contentString.equals("")) {
            contentString = contentString.substring(0, contentString.length() - 2);
        }
        return contentString;
    }

    /**
     * 数字转布尔类型
     */
    public static boolean getNum2Bool(String num) {
        boolean flag = true;
        if ("1".equals(num)) {
            flag = false;
        }
        return flag;
    }

    /**
     * 数字转英文大写
     */
    public static String parseNumber(String x) {
        if (x == null || x.equals("")) {
            return "";
        }
        if (Double.parseDouble(x) <= 0) {
            return "Zero only";
        }
        // 取小数点位置
        int z = x.indexOf(".");
        String lstr = "", rstr = "";
        if (z > -1) {
            // 看是否有小数，如果有，则分别取左边和右边
            lstr = x.substring(0, z);
            rstr = x.substring(z + 1);
        } else
        // 否则就是全部
        {
            lstr = x;
        }
        String lstrrev = reverse(lstr);
        // 对左边的字串取反
        String[] a = new String[5];
        // 定义5个字串变量来存放解析出来的叁位一组的字串
        switch (lstrrev.length() % 3) {
            case 1:
                lstrrev += "00";
                break;
            case 2:
                lstrrev += "0";
                break;
            default:
        }
        String lm = "";
        // 用来存放转换後的整数部分
        for (int i = 0; i < lstrrev.length() / 3; i++) {
            a[i] = reverse(lstrrev.substring(3 * i, 3 * i + 3));
            // 截取第一个叁位
            if (!a[i].equals("000")) {
                // 用来避免这种情况：1000000 = one million
                if (i != 0) {
                    lm = transThree(a[i]) + " " + parseMore(String.valueOf(i)) + " " + lm; // 加:
                }
                // thousand、million、billion
                else {
                    lm = transThree(a[i]);
                }
                // 防止i=0时， 在多加两个空格.
            } else {
                lm += transThree(a[i]);
            }
        }
        String xs = "";
        // 用来存放转换後小数部分
        if (z > -1) {
            String transTwo = transTwo(rstr);
            if (transTwo == null || "".equals(transTwo)) {
                xs = "";
            } else {
                xs = " AND " + transTwo;
                // 小数部分存在时转换小数
            }
        }
        return lm.trim() + xs;
    }

    public static String parseFirst(String s) {
        String[] SINGLE_NUM_ARR = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
        return SINGLE_NUM_ARR[Integer.parseInt(s.substring(s.length() - 1))];
    }

    public static String parseTeen(String s) {
        String[] TEN_NUM_ARR = new String[]{"Ten", "Eleven", "Tweleve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
        return TEN_NUM_ARR[Integer.parseInt(s) - 10];
    }

    public static String parseTen(String s) {
        String[] TEN_INTEGER_ARR = new String[]{"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        return TEN_INTEGER_ARR[Integer.parseInt(s.substring(0, 1)) - 1];
    }

    public static String transTwo(String s) {
        String value = "";
        // 判断位数
        if (s.length() > 2) {
            s = s.substring(0, 2);
        } else if (s.length() < 2) {
            s = s + "0";
        }
        if (s.startsWith("0"))
        // 07 - seven 是否小於10
        {
            value = parseFirst(s);
        } else if (s.startsWith("1"))
        // 17 seventeen 是否在10和20之间
        {
            value = parseTeen(s);
        }
        // 是否在10与100之间的能被10整除的数
        else if (s.endsWith("0")) {
            value = parseTen(s);
        } else {
            value = parseTen(s) + "-" + parseFirst(s);
        }
        return value;
    }

    public static String transThree(String s) {
        String value = "";
        if (s.startsWith("0"))
        // 是否小於100
        {
            value = transTwo(s.substring(1));
        }
        // 是否被100整除
        else if (s.substring(1).equals("00")) {
            value = parseFirst(s.substring(0, 1)) + " Hundred";
        } else {
            value = parseFirst(s.substring(0, 1)) + " Hundred and " + transTwo(s.substring(1));
        }
        return value;
    }

    public static String parseMore(String s) {
        String[] a = new String[]{"", "Thousand,", "Million,", "Billion,"};
        return a[Integer.parseInt(s)];
    }

    /**
     *  取反
     */
    public static String reverse(String s) {
        char[] aChr = s.toCharArray();
        StringBuffer tmp = new StringBuffer();
        for (int i = aChr.length - 1; i >= 0; i--) {
            tmp.append(aChr[i]);
        }
        return tmp.toString();
    }

    /**
     * double 金额转大写
     */
    public static String digitCapital(double n) {
        String fraction[] = {"角", "分"};
        String digit[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String unit[][] = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};
        String head = n < 0 ? "负" : "";
        // 如果是负数取绝对值
        n = Math.abs(n);
        String s = "";
        BigDecimal bigDecimal = new BigDecimal(Double.valueOf(n).toString());
        String nStr = bigDecimal.toString();
        // 小数部分
        String[] split = nStr.split("\\.");
        if (split.length > 1) {
            // 小数点为特殊符号，在分割时需进行转义
            String decimalStr = split[1];
            if (decimalStr.length() > 2) {
                decimalStr = decimalStr.substring(0, 2);
            }
            // 将小数部分转换为整数
            Integer integer = Integer.valueOf(decimalStr);
            String p = "";
            for (int i = 0; i < decimalStr.length() && i < fraction.length; i++) {
                p = digit[integer % 10] + fraction[decimalStr.length() - i - 1] + p;
                integer = integer / 10;
            }
            s = p.replaceAll("(零.)+", "") + s;
        }
        if (s.length() < 1) {
            s = "整";
        }
        int integerPart = (int) Math.floor(n);
        // 整数部分
        for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
            String p = "";
            for (int j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[integerPart % 10] + unit[1][j] + p;
                integerPart = integerPart / 10;
            }
            s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
        }
        return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
    }


    public static void main(String[] args) {
        System.out.println(digitCapital(533));
    }
}
